package com.dcloud.manage.dbsource.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dcloud.enums.BizErrorEnum;
import com.dcloud.exception.BizException;
import com.dcloud.manage.request.SourceParamsRequest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.dcloud.manage.service.entity.CollectSourceDefine;
import com.dcloud.manage.service.ICollectSourceDefineService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 数据源定义表
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Api(tags="数据源定义表")
@RestController
@RequestMapping("/api/collectSourceDefine/v1")
@Slf4j
public class CollectSourceDefineController extends JeecgController<CollectSourceDefine, ICollectSourceDefineService> {
	@Autowired
	private ICollectSourceDefineService collectSourceDefineService;
	
	/**
	 * 分页列表查询
	 *
	 * @param collectSourceDefine
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "数据源定义表-分页列表查询")
	@ApiOperation(value="数据源定义表-分页列表查询", notes="数据源定义表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CollectSourceDefine collectSourceDefine,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CollectSourceDefine> queryWrapper = QueryGenerator.initQueryWrapper(collectSourceDefine, req.getParameterMap());
		Page<CollectSourceDefine> page = new Page<CollectSourceDefine>(pageNo, pageSize);
		IPage<CollectSourceDefine> pageList = collectSourceDefineService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param collectSourceDefine
	 * @return
	 */
	@AutoLog(value = "数据源定义表-添加")
	@ApiOperation(value="数据源定义表-添加", notes="数据源定义表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CollectSourceDefine collectSourceDefine) {
		collectSourceDefineService.save(collectSourceDefine);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param collectSourceDefine
	 * @return
	 */
	@AutoLog(value = "数据源定义表-编辑")
	@ApiOperation(value="数据源定义表-编辑", notes="数据源定义表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CollectSourceDefine collectSourceDefine) {
		collectSourceDefineService.updateById(collectSourceDefine);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "数据源定义表-通过id删除")
	@ApiOperation(value="数据源定义表-通过id删除", notes="数据源定义表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Integer id) {
		if(id == null){
			throw new BizException(BizErrorEnum.REQUSET_ERROR_NULL);
		}

		//删除数据源定义时，先判断是否存在该定义关联的数据源，如果存在就不能删除
		collectSourceDefineService.deleteById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "数据源定义表-批量删除")
	@ApiOperation(value="数据源定义表-批量删除", notes="数据源定义表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.collectSourceDefineService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "数据源定义表-通过id查询")
	@ApiOperation(value="数据源定义表-通过id查询", notes="数据源定义表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CollectSourceDefine collectSourceDefine = collectSourceDefineService.getById(id);
		if(collectSourceDefine==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(collectSourceDefine);
	}

	 /**
	  * 查询所有数据源类型
	  *
	  * @param
	  * @return
	  */
	 @RequestMapping(value = "/getAllList", method = RequestMethod.POST)
	 public Result<?> getListBySourceId() {
		 return Result.OK(collectSourceDefineService.getAll());
	 }


 }
